<template>
  <div v-if="goods" class="goods-page">
    <div class="container">
      <!-- 面包屑 -->
      <RbtBread>
        <RbtBreadItem to="/">首页</RbtBreadItem>
        <RbtBreadItem :to="`/category/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</RbtBreadItem>
        <RbtBreadItem :to="`/category/sub/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</RbtBreadItem>
        <RbtBreadItem>{{ goods.name }}</RbtBreadItem>
      </RbtBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <!-- 名字区组件 -->
          <GoodsName :goods="goods" />
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku" />
          <RbtNumbox
            v-model="num"
            label="数量"
            :max="goods.inventory"
            @change="changeNum"
          />
          <RbtButton type="primary" style="margin-top: 20px" @click="insertCart"
            >加入购物车</RbtButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods="goods" />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { GoodsType, SkuType } from '@/types/goods'
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import Message from '@/components/Message'
import useStore from '@/store'
const route = useRoute()
const goods = ref<GoodsType>()

// 获取商品详情
const useGoods = () => {
  // 出现路由地址商品id发生变化, 但是不会初始化组件
  findGoods(route.params.id as string).then(res => {
    goods.value = null
    nextTick(() => {
      goods.value = res.data.result
    })
  })
}

// 选择的数量
const num = ref(1)
const changeNum = (num: number) => {
  console.log(num)
}

watch(
  () => route.params.id,
  newVal => {
    if (newVal && `/product/${newVal}` === route.path) {
      useGoods()
    }
  },
  { immediate: true }
)

// sku改变时候触发
const changeSku = (sku: SkuType) => {
  if (sku.skuId) {
    goods.value.price = sku.price
    goods.value.oldPrice = sku.oldPrice
    goods.value.inventory = sku.inventory
    currSku.value = sku
  } else {
    currSku.value = null
  }
}

// 加入购物车
const currSku = ref<SkuType>(null)
const { cart } = useStore()
const insertCart = () => {
  if (!currSku.value) {
    return Message({ type: 'warn', text: '请选择物品规格' })
  }
  if (num.value > goods.value.inventory) {
    Message({ type: 'warn', text: '库存不足' })
  }
  cart
    .insertCartAsync({
      id: goods.value.id,
      skuId: currSku.value.skuId,
      name: goods.value.name,
      picture: goods.value.mainPictures[0],
      price: currSku.value.price,
      nowPrice: currSku.value.price,
      count: num.value,
      attrsText: currSku.value.specsText,
      selected: true,
      stock: currSku.value.inventory,
      isEffective: true
    })
    .then(() => {
      Message({ type: 'success', text: '加入购物车' })
    })
}

// 给孙组件传值
provide('goods', goods)
</script>

<style scoped lang="scss">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
</style>
